package com.jxc.service.impl;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.BasicArgument;
import com.jxc.model.po.Goods;
import com.jxc.model.po.Member;
import com.jxc.model.po.MemberType;
import com.jxc.model.po.PointUsedRecord;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.SaleReturnlist;
import com.jxc.model.po.SaleReturnlistItem;
import com.jxc.model.po.Salelist;
import com.jxc.model.po.SalelistItem;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Print;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VGoods;
import com.jxc.model.vo.VGoodsRepertory;
import com.jxc.model.vo.VMember;
import com.jxc.model.vo.VMemberType;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VSaleReturnlist;
import com.jxc.model.vo.VSaleReturnlistItem;
import com.jxc.model.vo.VUser;
import com.jxc.service.GoodsRepertoryServiceI;
import com.jxc.service.MemberServiceI;
import com.jxc.service.MemberTypeServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.SaleReturnlistServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.Constants;
import com.jxc.util.ResourceUtil;
import com.jxc.util.SaleReturnInfo;

@Service("saleReturnlistService")
public class SaleReturnlistServiceImpl implements SaleReturnlistServiceI {

	@Autowired
	private BaseDaoI<Salelist> salelistDao;
	@Autowired
	private BaseDaoI<SalelistItem> salelistItemDao;
	@Autowired
	private BaseDaoI<SaleReturnlist> saleReturnlistDao;
	@Autowired
	private BaseDaoI<SaleReturnlistItem> saleReturnlistItemDao;
	@Autowired
	private BaseDaoI<BasicArgument> basicArumentDao;
	@Autowired
	private BaseDaoI<PointUsedRecord> pointUsedRecorDao;
	@Autowired
	private BaseDaoI<Goods> goodsDao;
	@Autowired
	private BaseDaoI<Member> memberBaseDao;
	@Autowired
	private GoodsRepertoryServiceI goodsreService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private MemberServiceI memberService;
	@Autowired
	private MemberTypeServiceI memberTypeService;
	@Autowired
	private RepertoryServiceI repertoryService;

	private int memberPoint=0;	//会员应退积分统计 
	
	@Override
	public Print add(VSaleReturnlist vSaleReturnlist, String itemIds) {
		Print print = new Print();
		if(itemIds!=null&&!itemIds.trim().equals("")){
			
		Salelist salelist = salelistDao.get(Salelist.class, vSaleReturnlist.getSlid());
		//
		if (salelist != null) {
			SaleReturnlist saleReturnlist = new SaleReturnlist();
			BeanUtils.copyProperties(vSaleReturnlist, saleReturnlist);
			saleReturnlist.setSaleList(salelist);
			if (vSaleReturnlist.getUid() != null) {
				User user = new User();
				user.setId(vSaleReturnlist.getUid());
				saleReturnlist.setUser(user);
			}
			if (vSaleReturnlist.getRid() != null) {
				Repertory repertory = new Repertory();
				repertory.setId(vSaleReturnlist.getRid());
				saleReturnlist.setRepertory(repertory);
				//封装print数据start
				VRepertory vrepertory =  repertoryService.get(repertory.getId());
				if(vrepertory.getAddress()!=null){
					print.setRadd(vrepertory.getAddress());
				}
				if(vrepertory.getName()!=null){
					print.setRname(vrepertory.getName());
				}
				if(vrepertory.getPhone()!=null){
					print.setRtel(vrepertory.getPhone());
				}
				//封装print数据end

			}
			saleReturnlist.setDate(new Date());
			saleReturnlist.setMember(salelist.getMember());
			saleReturnlistDao.save(saleReturnlist);	//判断之前进行保存了
			saleReturnlist = saleReturnlistDao.get(SaleReturnlist.class, saleReturnlist.getId());
			Double total = 0d;
			//退款时附带的积分操作start
			Integer returnpoint = 0;
		
			//应退金额
			Double realReturn = 0d;
			String[] ids = itemIds.split(",");//itemIds存退货数量和退货金额
			String[] saleids = vSaleReturnlist.getIds().split(",");//销售明细id
			List<VSaleReturnlistItem> vsrlilist = new ArrayList<VSaleReturnlistItem>();//退货明细列表用于打印
			
			//ids
			for (int i = 0; i < saleids.length; i++) {
				SalelistItem item = salelistItemDao.get(SalelistItem.class, Long.parseLong(saleids[i]));
				if (item != null) {
					SaleReturnlistItem returnlistItem = new SaleReturnlistItem();
					returnlistItem.setGoods(item.getGoods());//商品
//					Integer sums = 0;
					Integer num = Integer.valueOf(ids[(i+1)*2-2]);
//					sums += num;
					returnlistItem.setNum(num);//退货数量
					returnlistItem.setTotal(Double.parseDouble(ids[(i+1)*2-1]));//退货金额
					total += returnlistItem.getTotal();
					returnlistItem.setSaleReturnlist(saleReturnlist);//退货单外键
//					item.setRefund(true);
					//操作销售记录
					item.setTotal(item.getTotal()-returnlistItem.getTotal());
					
					//为print对象封装退货明细数据start
					VSaleReturnlistItem vSaleReturnlistItem = new VSaleReturnlistItem();
					if(returnlistItem.getGoods()!=null){
						Goods goods = item.getGoods();
						if(goods.getName()!=null){
							vSaleReturnlistItem.setGname(item.getGoods().getName());
	
						if(goods.getType()!=null){
							vSaleReturnlistItem.setGtype(goods.getType());
						}
					}
						int num1=item.getNumber();
						int num2=num;
						
						//销售数量小于退货数量 抛出异常退货失败
						
							//判断退货数量是否合法
							if(num1<num2){
								
								print.setSign(SaleReturnInfo.FAIL);
								return print;
								
							}
							//判断合法性
							int result=num1-num2;
							
							if(result<0){

								print.setSign(SaleReturnInfo.FAIL);
								return print;
							}
							
							
							if(goods.getType()!=null){
								vSaleReturnlistItem.setGtype(goods.getType());
					
							}
						
					if(returnlistItem.getNum()!=null){
						vSaleReturnlistItem.setNum(returnlistItem.getNum());
					}
					if(returnlistItem.getTotal()!=null){
						vSaleReturnlistItem.setTotal(returnlistItem.getTotal());
					}
					vsrlilist.add(vSaleReturnlistItem);
					//为print对象封装退货明细数据 end
						
						//设置销售单的数量 - 退货数量
						item.setNumber(result);
			
						//更新销售单
					salelistItemDao.update(item);
					
					//保存退货list

					saleReturnlistItemDao.save(returnlistItem);
//					item.setRefund(true);
//					item.setTotal(item.getTotal()-returnlistItem.getTotal());
//					salelistItemDao.update(item);
					
					
//					退货完成后,将库存加上
					
					//goodsreService.addInventory(salelist.getRepertory().getId(),item.getGoods().getId(),num);
					
					//操作库存
					VGoodsRepertory vg = new VGoodsRepertory();
					vg.setIrid(vSaleReturnlist.getRid());
					String sLong = String.valueOf(num); 
					vg.setNum(Integer.parseInt(sLong));
					vg.setGid(item.getGoods().getId());
					goodsreService.findCounts(vg);
				}
			}
			
			
			
		}//for end
			
			
			//操作会员积分
			
			realReturn = total;
			vSaleReturnlist.setRealReturn(realReturn);
			if(salelist.getMember()!=null){
				Member member = salelist.getMember();
			
				
				if(salelist.getPointNum()==null)salelist.setPointNum(0);
				
				//抛出异常
				User tmp=salelist.getUser();
				if(tmp!=null&&(tmp.getPointDeductionBase()==null||tmp.getMoneyPointProp()==null||tmp.getPointMoneyProp()==null)){
					//默认
					//1 20 3000
					tmp.setPointMoneyProp(20);
					tmp.setMoneyPointProp(1);;
					tmp.setPointDeductionBase(3000);
				}
				
				if (salelist.getPointNum() > 0) {
					PointUsedRecord pointUsedRecord = getPointUsedRecordBySalelist(salelist);
					
					if(pointUsedRecord!=null&&pointUsedRecord.getNum()>0){
						
						
						Double amount = Double.valueOf(pointUsedRecord.getNum())/(salelist.getUser().getUser().getPointMoneyProp()!=null?salelist.getUser().getUser().getPointMoneyProp():tmp.getPointMoneyProp());
						
						if(realReturn>amount){//积分兑换金少于退款金额
							realReturn = realReturn-amount;
							returnpoint = pointUsedRecord.getNum()-(int)Math.floor(realReturn/(salelist.getUser().getUser().getMoneyPointProp()!=null?salelist.getUser().getUser().getMoneyPointProp():tmp.getMoneyPointProp()));
							member.setPoint(member.getPoint()+returnpoint);
							pointUsedRecorDao.delete(pointUsedRecord);
							member.setConsume(member.getConsume()-realReturn);
						}else{//积分兑换金大于退款金额，退部分积分即可
							Integer num = (int)(realReturn*(salelist.getUser().getUser().getPointMoneyProp()!=null?salelist.getUser().getUser().getPointMoneyProp():tmp.getPointMoneyProp()));
							pointUsedRecord.setNum(pointUsedRecord.getNum()-num);
							realReturn = realReturn-amount;
							pointUsedRecorDao.update(pointUsedRecord);
							returnpoint = num-realReturn.intValue();
							member.setPoint(member.getPoint()+returnpoint);
						}
					}
				}else{//未使用积分支付的情况
					if(salelist.getMember()!=null){
						if(member.getPoint()==null)member.setPoint(0);
						if(salelist.getUser().getMoneyPointProp()!=null){
							returnpoint = -(int)(realReturn/(salelist.getUser().getUser().getMoneyPointProp()!=null?salelist.getUser().getUser().getMoneyPointProp():tmp.getMoneyPointProp()));
							member.setPoint(member.getPoint()+returnpoint);
							member.setConsume(member.getConsume()-realReturn);
						}
					}
				}
				
				vSaleReturnlist.setReturnPoint(returnpoint);
				//退款时附带的积分操作end
				
				
				//会员升级功能 start
				if((member.getMemberType()!=null&&member.getMemberType().getMaxAmount()!=null&&member.getConsume()>member.getMemberType().getMaxAmount())
						||(member.getMemberType()!=null&&member.getMemberType().getMinAmount()!=null&&member.getConsume()<member.getMemberType().getMinAmount())){
					SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
					VUser vuser = userService.get(sessionInfo.getUserId());
					VMemberType vmt = new VMemberType();
					if(vuser.getUserType()==1||vuser.getUserType()==5){
						vmt.setUid(vuser.getId());
					}
					if(vuser.getUpid()!=null&&(vuser.getUserType()==3||vuser.getUserType()==4)&&(userService.get(vuser.getUpid()).getUserType()==1||userService.get(vuser.getUpid()).getUserType()==5)){
						vmt.setUid(vuser.getUpid());
					}
					List<VMemberType> vmts = memberTypeService.combobox(vmt);
					for(VMemberType vmt1 : vmts){
						if(member.getConsume()>vmt1.getMinAmount()){
							if(vmt1.getMaxAmount()!=null){
								if(member.getConsume()<vmt1.getMaxAmount()){
									MemberType mt = new MemberType();
									BeanUtils.copyProperties(vmt1, mt);
									member.setMemberType(mt);
								}
							}else{
								MemberType mt = new MemberType();
								BeanUtils.copyProperties(vmt1, mt);
								member.setMemberType(mt);
							}
						}
					}
				}
				//会员升级功能 end
				
				memberBaseDao.update(member);
			}
			
			//会员积分end
			
			
			saleReturnlist.setTotal(total);
			saleReturnlist.setRealReturn(realReturn);
			saleReturnlistDao.update(saleReturnlist);
			
			
			//封装打印数据start
			if(vSaleReturnlist.getUusername()!=null){
				print.setUname(vSaleReturnlist.getUusername());
			}
			if(vSaleReturnlist.getDate()!=null){
				//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				//设置时间
				print.setSalereturntime(new Date().toLocaleString());
			}
			if(vSaleReturnlist.getSerial()!=null){
				print.setSalereturnserial(vSaleReturnlist.getSerial());
			}
			//应退金额
			if(vSaleReturnlist.getRealReturn()!=null){
				print.setReturnAmount(realReturn);
			}
			//设置应退积分
			if(vSaleReturnlist.getReturnPoint()!=null){
				//前台和后台的积分相差1
				//memberPoint+=vSaleReturnlist.getReturnPoint();
				print.setReturnPoint(vSaleReturnlist.getReturnPoint()-1);
			}
		
			print.setVsrlisForPrint(vsrlilist);
			//封装打印数据end
			
		}
		}
		else{
			
			return print;
		}
		return print;

	}
	

	
	private PointUsedRecord getPointUsedRecordBySalelist(Salelist salelist){
		String hql = "from PointUsedRecord t where t.salelist.id=?";
		List<Object> values = new ArrayList<Object>();
		values.add(salelist.getId());
		return pointUsedRecorDao.get(hql, values);
	}

	private BasicArgument getBasricArgumentByKey(Long rid, String key) {
		String hql = "from BasicArgument t where t.repertory.id=? and t.argKey=? ";
		List<Object> values = new ArrayList<Object>();
		values.add(rid);
		values.add(key);
		return basicArumentDao.get(hql, values);
	}

	private List<VSaleReturnlist> changeModel(List<SaleReturnlist> srls) {
		List<VSaleReturnlist> vsrls = new ArrayList<VSaleReturnlist>();
		if (srls != null && srls.size() > 0) {
			for (SaleReturnlist srl : srls) {
				VSaleReturnlist vsrl = new VSaleReturnlist();
				BeanUtils.copyProperties(srl, vsrl);
				if (srl.getUser() != null) {
					User user = srl.getUser();
					vsrl.setUid(user.getId());
					vsrl.setUusername(user.getUsername());
				}
				if (srl.getRepertory() != null) {
					Repertory repertory = srl.getRepertory();
					vsrl.setRid(repertory.getId());
					vsrl.setRname(repertory.getName());
				}
				if (srl.getSaleList() != null) {
					Salelist sl = srl.getSaleList();
					vsrl.setSlid(sl.getId());
				}
				if (srl.getMember() != null) {
					Member member = srl.getMember();
					vsrl.setMid(member.getId());
					vsrl.setMname(member.getName());
				}
				vsrls.add(vsrl);
			}
		}
		return vsrls;
	}

	private List<SaleReturnlist> find(VSaleReturnlist vsrl) {
		String hql = "from SaleReturnlist t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vsrl, hql, values);

		if (vsrl.getSort() != null && vsrl.getOrder() != null) {
			hql += " order by " + vsrl.getSort() + " " + vsrl.getOrder();
		}
		return this.saleReturnlistDao.find(hql, values, vsrl.getPage(), vsrl.getRows());
	}

	private Long total(VSaleReturnlist vbrand) {
		String hql = "select count(*) from SaleReturnlist t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vbrand, hql, values);
		return saleReturnlistDao.count(hql, values);
	}

	private String addWhere(VSaleReturnlist vsrl, String hql, List<Object> values) {
		if (vsrl.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vsrl.getId());
		}

		if (vsrl.getSerial() != null) {
			hql += " and t.serial = ? ";
			values.add(vsrl.getSerial());
		}

		if (vsrl.getBeginDate() != null) {
			hql += " and t.date >= ? ";
			values.add(vsrl.getBeginDate());
		}

		if (vsrl.getEndDate() != null) {
			hql += " and t.date <= ? ";
			values.add(vsrl.getEndDate());
		}

		if (vsrl.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(vsrl.getUid());
		}

		if (vsrl.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(vsrl.getRid());
		}

		return hql;
	}

	@Override
	public List<VSaleReturnlist> combobox(VSaleReturnlist vsrl) {
		return this.changeModel(this.find(vsrl));
	}

	@Override
	public DataGrid datagrid(VSaleReturnlist vsrl) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vsrl)));
		dg.setTotal(this.total(vsrl));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					SaleReturnlist srl = this.saleReturnlistDao.get(SaleReturnlist.class, Long.parseLong(id.trim()));
					if (srl != null) {
						saleReturnlistDao.update(srl);
					}
				}
			}
		}
	}

	@Override
	public void update(VSaleReturnlist vsrl) {
		SaleReturnlist srl = saleReturnlistDao.get(SaleReturnlist.class, vsrl.getId());
		BeanUtils.copyProperties(vsrl, srl, new String[] { "id" });
		if (vsrl.getRid() != null) {
			srl.setRepertory(new Repertory(vsrl.getRid()));
		}
		if (vsrl.getUid() != null) {
			srl.setUser(new User(vsrl.getUid()));
		}
		if (vsrl.getMid() != null) {
			srl.setMember(new Member(vsrl.getMid()));
		}
		if (vsrl.getSlid() != null) {
			srl.setSaleList(new Salelist(vsrl.getSlid()));
		}
		this.saleReturnlistDao.update(srl);
	}

	@Override
	public Print saveOrUpdate(VSaleReturnlist vSaleReturnlist, String itemIds) {
		Print print = new Print();
		try {
			if(itemIds!=null&&!itemIds.trim().equals("")){
			Salelist salelist = salelistDao.get(Salelist.class, vSaleReturnlist.getSlid());
			SaleReturnlist saleReturnlist = saleReturnlistDao.get(SaleReturnlist.class, vSaleReturnlist.getId());
			if (salelist != null) {
				BeanUtils.copyProperties(vSaleReturnlist, saleReturnlist);
				saleReturnlist.setSaleList(salelist);
				User user = new User();
				if (vSaleReturnlist.getUid() != null) {
					user.setId(vSaleReturnlist.getUid());
					saleReturnlist.setUser(user);
					user = saleReturnlist.getUser();
				}
				if (vSaleReturnlist.getRid() != null) {
					Repertory repertory = new Repertory();
					repertory.setId(vSaleReturnlist.getRid());
					saleReturnlist.setRepertory(repertory);
					//封装print数据start
					VRepertory vrepertory =  repertoryService.get(repertory.getId());
					if(vrepertory.getAddress()!=null){
						print.setRadd(vrepertory.getAddress());
					}
					if(vrepertory.getName()!=null){
						print.setRname(vrepertory.getName());
					}
					if(vrepertory.getPhone()!=null){
						print.setRtel(vrepertory.getPhone());
					}
					//封装print数据end

				}
				saleReturnlist.setDate(new Date());
				saleReturnlist.setMember(salelist.getMember());
				String name="";
				if(user!=null){
					name=user.getRealName();
				}else{
					name="匿名";
				}
				saleReturnlist.setRemark(saleReturnlist.getRemark()+" "+name+"退货");
				saleReturnlistDao.update(saleReturnlist);
				saleReturnlist = saleReturnlistDao.get(SaleReturnlist.class, saleReturnlist.getId());
				Double total = 0d;
				Double realReturn = 0d;
				String[] ids = itemIds.split(",");//itemIds存退货数量和退货金额
				String[] saleids = vSaleReturnlist.getIds().split(",");//销售明细id
				List<VSaleReturnlistItem> vsrlilist = new ArrayList<VSaleReturnlistItem>();//退货明细列表用于打印
				for (int i = 0; i < saleids.length; i++) {
					SalelistItem item = salelistItemDao.get(SalelistItem.class, Long.parseLong(saleids[i]));
					if(item!=null){
						System.out.println(vSaleReturnlist.getSritemsid());
						for(String stid: vSaleReturnlist.getSritemsid().split(",")){
//							if(stid!="0"&&!stid.equals("0")){
								SaleReturnlistItem returnlistItem = saleReturnlistItemDao.get(SaleReturnlistItem.class, new Long(stid));
								
//								再次退货之前,先把库存变回没退货之前的
								VGoodsRepertory vg = new VGoodsRepertory();
								vg.setOrid(vSaleReturnlist.getRid());
								
								//库存数量+退货的商品
								vg.setNum(returnlistItem.getNum());
								vg.setGid(item.getGoods().getId());
								goodsreService.findOut(vg);
//								如果修改的是之前退货过的商品,则在原本的退货明细中修改
								Double oldTotal=0d;
								if(returnlistItem.getTotal()!=null){
									oldTotal = returnlistItem.getTotal();
									System.out.println(oldTotal);
								}
							
								
								
								returnlistItem.setGoods(item.getGoods());//商品
								Integer num = Integer.valueOf(ids[i]);
								returnlistItem.setNum(num);//退货数量
								returnlistItem.setTotal(Double.parseDouble(ids[(i+1)*2-1]));//退货金额
								returnlistItem.setSaleReturnlist(saleReturnlist);//退货单外键
								

								//为print对象封装退货明细数据start
								VSaleReturnlistItem vSaleReturnlistItem = new VSaleReturnlistItem();
								if(returnlistItem.getGoods()!=null){
									Goods goods = item.getGoods();
									if(goods.getName()!=null){
										vSaleReturnlistItem.setGname(item.getGoods().getName());
									}
									if(goods.getType()!=null){
										vSaleReturnlistItem.setGtype(goods.getType());
									}
								//修改销售记录
								int num1=item.getNumber();
								int num2=num;
								
								//销售数量小于退货数量 抛出异常退货失败
								if(num1<num2){
									print.setSign(SaleReturnInfo.FAIL);
									return print;
								}
								if(returnlistItem.getNum()!=null){
									vSaleReturnlistItem.setNum(returnlistItem.getNum());
								
								int result=num1-num2;
								
								if(result<0){
									print.setSign(SaleReturnInfo.FAIL);
									return print;
								}
								if(returnlistItem.getTotal()!=null){
									vSaleReturnlistItem.setTotal(returnlistItem.getTotal());
								}
								vsrlilist.add(vSaleReturnlistItem);
								//为print对象封装退货明细数据 end
								
								item.setNumber(result);
								salelistItemDao.update(item);
								
								returnlistItem.setTotal(Double.parseDouble(ids[++i]));//退货金额
								returnlistItem.setSaleReturnlist(saleReturnlist);//退货单外键
								saleReturnlistItemDao.update(returnlistItem);
								total += (returnlistItem.getTotal()-oldTotal);//累加退货金额
								System.out.println(returnlistItem.getTotal()+","+oldTotal);
//								退货完成后,将库存加上
								
								//goodsreService.addInventory(salelist.getRepertory().getId(),item.getGoods().getId(),num);
								
								//清理缓存 会影响其他的
								//salelistDao.clean();
								VGoodsRepertory vg2 = new VGoodsRepertory();
								vg2.setIrid(vSaleReturnlist.getRid());
								String sLong2 = String.valueOf(num); 
								vg2.setNum(Integer.parseInt(sLong2));
								vg2.setGid(item.getGoods().getId());
								goodsreService.findCounts(vg2);
						}
					}
				}

				saleReturnlistDao.update(saleReturnlist);
				
			}
			
			}//for end
				//退货操作 Begin 
				realReturn = total;
				vSaleReturnlist.setRealReturn(realReturn);
				if(salelist.getMember()!=null){
					Member member = salelist.getMember();
					
					//抛出异常
					User tmp=salelist.getUser();
					if(tmp!=null&&(tmp.getPointDeductionBase()==null||tmp.getMoneyPointProp()==null||tmp.getPointMoneyProp()==null)){
						//默认
						//1 20 3000
						tmp.setPointMoneyProp(20);
						tmp.setMoneyPointProp(1);;
						tmp.setPointDeductionBase(3000);
					}
					
					//退款时附带的积分操作
					Integer returnpoint = 0;
					if(salelist.getPointNum()==null)salelist.setPointNum(0);
					if (salelist.getPointNum() > 0) {
						PointUsedRecord pointUsedRecord = getPointUsedRecordBySalelist(salelist);
						if(pointUsedRecord!=null&&pointUsedRecord.getNum()>0){
							
							
							Double amount = Double.valueOf(pointUsedRecord.getNum())/(salelist.getUser().getUser().getPointMoneyProp()!=null?salelist.getUser().getUser().getPointMoneyProp():tmp.getPointMoneyProp());
							if(realReturn>amount){//积分兑换金少于退款金额
								realReturn = realReturn-amount;
								returnpoint = pointUsedRecord.getNum()-(int)Math.floor(realReturn/(salelist.getUser().getUser().getMoneyPointProp()!=null?salelist.getUser().getUser().getMoneyPointProp():tmp.getMoneyPointProp()));
								member.setPoint(member.getPoint()+returnpoint);
								pointUsedRecorDao.delete(pointUsedRecord);
								member.setConsume(member.getConsume()-realReturn);
							}else{//积分兑换金大于退款金额，退部分积分即可
								Integer num = (int)(realReturn*(salelist.getUser().getUser().getPointMoneyProp()!=null?salelist.getUser().getUser().getPointMoneyProp():tmp.getPointDeductionBase()));
								pointUsedRecord.setNum(pointUsedRecord.getNum()-num);
								realReturn = realReturn-amount;
								pointUsedRecorDao.update(pointUsedRecord);
								returnpoint = num-realReturn.intValue();
								member.setPoint(member.getPoint()+returnpoint);
							}
						}
					}else{//未使用积分支付的情况
						if(salelist.getMember()!=null){
							if(member.getPoint()==null)member.setPoint(0);
							if(salelist.getUser().getMoneyPointProp()!=null){
								returnpoint = -(int)(realReturn/(salelist.getUser().getUser().getMoneyPointProp()!=null?realReturn/salelist.getUser().getUser().getMoneyPointProp():tmp.getMoneyPointProp()));
								member.setPoint(member.getPoint()+returnpoint);
							}
						}
					}
					vSaleReturnlist.setReturnPoint(returnpoint);
					
					//会员升级功能 start
					if((member.getMemberType()!=null&&member.getMemberType().getMaxAmount()!=null&&member.getConsume()>member.getMemberType().getMaxAmount())
							||(member.getMemberType()!=null&&member.getMemberType().getMinAmount()!=null&&member.getConsume()<member.getMemberType().getMinAmount())){
						SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
						VUser vuser = userService.get(sessionInfo.getUserId());
						VMemberType vmt = new VMemberType();
						if(vuser.getUserType()==1||vuser.getUserType()==5){
							vmt.setUid(vuser.getId());
						}
						if(vuser.getUpid()!=null&&(vuser.getUserType()==3||vuser.getUserType()==4)&&(userService.get(vuser.getUpid()).getUserType()==1||userService.get(vuser.getUpid()).getUserType()==5)){
							vmt.setUid(vuser.getUpid());
						}
						List<VMemberType> vmts = memberTypeService.combobox(vmt);
						for(VMemberType vmt1 : vmts){
							if(member.getConsume()>vmt1.getMinAmount()){
								if(vmt1.getMaxAmount()!=null){
									if(member.getConsume()<vmt1.getMaxAmount()){
										MemberType mt = new MemberType();
										BeanUtils.copyProperties(vmt1, mt);
										member.setMemberType(mt);
									}
								}else{
									MemberType mt = new MemberType();
									BeanUtils.copyProperties(vmt1, mt);
									member.setMemberType(mt);
								}
							}
						}
					}
					//会员升级功能 end
					
					memberBaseDao.update(member);
				}
				
				
				
				saleReturnlist.setTotal(total);
				saleReturnlist.setRealReturn(realReturn);
				saleReturnlistDao.update(saleReturnlist);
				
				//封装打印数据start
				if(vSaleReturnlist.getUusername()!=null){
					print.setUname(vSaleReturnlist.getUusername());
				}
				if(vSaleReturnlist.getDate()!=null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					print.setSalereturntime(sdf.format(vSaleReturnlist.getDate()));
				}
				if(vSaleReturnlist.getSerial()!=null){
					print.setSalereturnserial(vSaleReturnlist.getSerial());
				}
				if(vSaleReturnlist.getRealReturn()!=null){
					print.setReturnAmount(vSaleReturnlist.getRealReturn());
				}
				//会员应退积分
				if(vSaleReturnlist.getReturnPoint()!=null){
					print.setReturnPoint(vSaleReturnlist.getReturnPoint()-1);
				}
				print.setVsrlisForPrint(vsrlilist);
				
				//封装打印数据end
				//设置会员的积分
				
			}
			}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		return print;

	}

	public Print addBySrid(VSaleReturnlist vSaleReturnlist, String itemIds) {
		Print print = new Print();
		try {
		if(itemIds!=null&&!itemIds.trim().equals("")){
			Salelist salelist = salelistDao.get(Salelist.class, vSaleReturnlist.getSlid());
			SaleReturnlist saleReturnlist = saleReturnlistDao.get(SaleReturnlist.class, vSaleReturnlist.getId());
			if (salelist != null) {
				BeanUtils.copyProperties(vSaleReturnlist, saleReturnlist);
				saleReturnlist.setSaleList(salelist);
				User user = new User();
				if (vSaleReturnlist.getUid() != null) {
					user.setId(vSaleReturnlist.getUid());
					saleReturnlist.setUser(user);
					user = saleReturnlist.getUser();
				}
				if (vSaleReturnlist.getRid() != null) {
					Repertory repertory = new Repertory();
					repertory.setId(vSaleReturnlist.getRid());
					saleReturnlist.setRepertory(repertory);
					
					//封装print数据start
					VRepertory vrepertory =  repertoryService.get(repertory.getId());
					if(vrepertory.getAddress()!=null){
						print.setRadd(vrepertory.getAddress());
					}
					if(vrepertory.getName()!=null){
						print.setRname(vrepertory.getName());
					}
					if(vrepertory.getPhone()!=null){
						print.setRtel(vrepertory.getPhone());
					}
					//封装print数据end

				}

				saleReturnlist.setDate(new Date());
				saleReturnlist.setMember(salelist.getMember());
				String name="";
				if(user!=null){
					name=user.getRealName();
				}else{
					name="匿名";
				}
				saleReturnlist.setRemark(saleReturnlist.getRemark()+" "+name+"退货");
				saleReturnlistDao.update(saleReturnlist);
				saleReturnlist = saleReturnlistDao.get(SaleReturnlist.class, saleReturnlist.getId());
				
				Double total = 0d;
				//应退金额
				Double realReturn = 0d;
				//退款时附带的积分操作
				Integer returnpoint = 0;
				
				String[] ids = itemIds.split(",");//itemIds存退货数量和退货金额
				String[] saleids = vSaleReturnlist.getIds().split(",");//销售明细id
				List<VSaleReturnlistItem> vsrlilist = new ArrayList<VSaleReturnlistItem>();//退货明细列表用于打印
				for (int i = 0; i < saleids.length; i++) {
					SalelistItem item = salelistItemDao.get(SalelistItem.class, Long.parseLong(saleids[i]));
					if(item!=null){
//						if(stid!="0"&&!stid.equals("0")){
						SaleReturnlistItem returnlistItem = new SaleReturnlistItem();
						returnlistItem.setGoods(item.getGoods());//商品
//						Integer sums = 0;
						
							Integer num = Integer.valueOf(ids[i]);
							
							//修改销售记录
							int num1=item.getNumber();
							int num2=num;
							
							//销售数量小于退货数量 抛出异常退货失败
							if(num1<num2){
								print.setSign(SaleReturnInfo.FAIL);
								return print;
							}
							
							int result=num1-num2;
							
							if(result<0){
								print.setSign(SaleReturnInfo.FAIL);
								return print;
							}
							
							item.setNumber(result);
							salelistItemDao.update(item);
							
	//						sums += num;
							returnlistItem.setNum(num);//退货数量
							returnlistItem.setTotal(Double.parseDouble(ids[++i]));//退货金额
						
							total += returnlistItem.getTotal();
							returnlistItem.setSaleReturnlist(saleReturnlist);//退货单外键
							
							//为print对象封装退货明细数据start
							VSaleReturnlistItem vSaleReturnlistItem = new VSaleReturnlistItem();
							if(returnlistItem.getGoods()!=null){
								Goods goods = item.getGoods();
								if(goods.getName()!=null){
									vSaleReturnlistItem.setGname(item.getGoods().getName());
								}
								if(goods.getType()!=null){
									vSaleReturnlistItem.setGtype(goods.getType());
								}
							}
							if(returnlistItem.getNum()!=null){
								vSaleReturnlistItem.setNum(returnlistItem.getNum());
							}
							if(returnlistItem.getTotal()!=null){
								vSaleReturnlistItem.setTotal(returnlistItem.getTotal());
							}
							vsrlilist.add(vSaleReturnlistItem);
							//为print对象封装退货明细数据 end
							
							saleReturnlistItemDao.save(returnlistItem);
	//						item.setRefund(true);
	//						item.setTotal(item.getTotal()-returnlistItem.getTotal());
	//						salelistItemDao.update(item);
							
//							退货完成后,将库存加上
							
							//goodsreService.addInventory(salelist.getRepertory().getId(),item.getGoods().getId(),num);
							
							VGoodsRepertory vg = new VGoodsRepertory();
							vg.setIrid(vSaleReturnlist.getRid());
							String sLong = String.valueOf(num); 
							vg.setNum(Integer.parseInt(sLong));
							vg.setGid(item.getGoods().getId());
							goodsreService.findCounts(vg);
						
					}
				}
				//退货总金额
				realReturn = total;
				vSaleReturnlist.setRealReturn(realReturn);
				if(salelist.getMember()!=null){
					Member member = salelist.getMember();
					
					//抛出异常
					User tmp=salelist.getUser();
					if(tmp!=null&&(tmp.getPointDeductionBase()==null||tmp.getMoneyPointProp()==null||tmp.getPointMoneyProp()==null)){
						//默认
						//1 20 3000
						tmp.setMoneyPointProp(1);
						tmp.setPointMoneyProp(20);
						tmp.setPointDeductionBase(3000);
					}
					
					if(salelist.getPointNum()==null)salelist.setPointNum(0);
					if (salelist.getPointNum() > 0) {
						PointUsedRecord pointUsedRecord = getPointUsedRecordBySalelist(salelist);
						if(pointUsedRecord!=null&&pointUsedRecord.getNum()>0){
						
							
							Double amount = Double.valueOf(pointUsedRecord.getNum())/(salelist.getUser().getUser().getPointMoneyProp()!=null?salelist.getUser().getUser().getPointMoneyProp():tmp.getPointMoneyProp());
							if(realReturn>amount){//积分兑换金少于退款金额
								//应退金额
								realReturn = realReturn-amount;
								//应退积分
								returnpoint = pointUsedRecord.getNum()-(int)Math.floor(realReturn/(salelist.getUser().getUser().getMoneyPointProp()!=null?salelist.getUser().getUser().getMoneyPointProp():tmp.getMoneyPointProp()));
								//改变会员的
								member.setPoint(member.getPoint()+returnpoint);
								pointUsedRecorDao.delete(pointUsedRecord);
								member.setConsume(member.getConsume()-realReturn);
							}else{//积分兑换金大于退款金额，退部分积分即可
								Integer num = (int)(realReturn*(salelist.getUser().getUser().getPointMoneyProp()!=null?salelist.getUser().getUser().getPointMoneyProp():tmp.getPointMoneyProp()));
								pointUsedRecord.setNum(pointUsedRecord.getNum()-num);
								realReturn = realReturn-amount;
								pointUsedRecorDao.update(pointUsedRecord);
								returnpoint = num-realReturn.intValue();
								member.setPoint(member.getPoint()+returnpoint);
							}
						}
					}else{//未使用积分支付的情况
						if(salelist.getMember()!=null){
							if(member.getPoint()==null)member.setPoint(0);
							if(salelist.getUser().getMoneyPointProp()!=null){
								returnpoint = -(int)(realReturn/(salelist.getUser().getUser().getMoneyPointProp()!=null?salelist.getUser().getUser().getMoneyPointProp():tmp.getMoneyPointProp()));
								member.setPoint(member.getPoint()+returnpoint);
							}
						}
					}
					vSaleReturnlist.setReturnPoint(returnpoint);
					
					//会员升级功能 start
					if((member.getMemberType()!=null&&member.getMemberType().getMaxAmount()!=null&&member.getConsume()>member.getMemberType().getMaxAmount())
							||(member.getMemberType()!=null&&member.getMemberType().getMinAmount()!=null&&member.getConsume()<member.getMemberType().getMinAmount())){
						SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
						VUser vuser = userService.get(sessionInfo.getUserId());
						VMemberType vmt = new VMemberType();
						if(vuser.getUserType()==1||vuser.getUserType()==5){
							vmt.setUid(vuser.getId());
						}
						if(vuser.getUpid()!=null&&(vuser.getUserType()==3||vuser.getUserType()==4)&&(userService.get(vuser.getUpid()).getUserType()==1||userService.get(vuser.getUpid()).getUserType()==5)){
							vmt.setUid(vuser.getUpid());
						}
						List<VMemberType> vmts = memberTypeService.combobox(vmt);
						for(VMemberType vmt1 : vmts){
							if(member.getConsume()>vmt1.getMinAmount()){
								if(vmt1.getMaxAmount()!=null){
									if(member.getConsume()<vmt1.getMaxAmount()){
										MemberType mt = new MemberType();
										BeanUtils.copyProperties(vmt1, mt);
										member.setMemberType(mt);
									}
								}else{
									MemberType mt = new MemberType();
									BeanUtils.copyProperties(vmt1, mt);
									member.setMemberType(mt);
								}
							}
						}
					}
					//会员升级功能 end
					memberBaseDao.update(member);

				}				
				
			
				
				//封装打印数据start
				if(vSaleReturnlist.getUusername()!=null){
					print.setUname(vSaleReturnlist.getUusername());
				}
				if(vSaleReturnlist.getDate()!=null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

					print.setSalereturntime(sdf.format(vSaleReturnlist.getDate()));
				}
				if(vSaleReturnlist.getSerial()!=null){
					print.setSalereturnserial(vSaleReturnlist.getSerial());
				}
				//应退的金额
				if(vSaleReturnlist.getRealReturn()!=null){
					print.setReturnAmount(realReturn);
				}
				//设置会员应退的积分
				if(vSaleReturnlist.getReturnPoint()!=null){
					
					//前台和后台计算出来的积分有1分的误差 测试过四次
					print.setReturnPoint(vSaleReturnlist.getReturnPoint()-1);
					
				}
				print.setVsrlisForPrint(vsrlilist);
				//封装打印数据end
				
				saleReturnlist.setTotal(total);
				
				//实际退的金额
				///saleReturnlist.setRealReturn(realReturn);
				saleReturnlistDao.update(saleReturnlist);
			 }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return print;
		
	}

	@Override
	public DataGrid datagridSaleReturn(VSaleReturnlist vsrl) {
		DataGrid da = new DataGrid();
		da.setRows(findSaleReturn(vsrl));
		da.setTotal(totalSaleReturn(vsrl));
		return da;
	}


	public List<VSaleReturnlist> findSaleReturn(VSaleReturnlist vsrl){
		String hql = " select  g.barcode,g.type,g.name gname,g.size,g.color,s.date,s.serial,r.id,r.name rname,s.member_id " +
		" from xsgl_salelist_item i ,xsgl_salelist s,rkgl_repertory r,spgl_goods g where i.goods_id=g.id and" +
		" i.salelist_id=s.id and s.repertory_id=r.id " ;
		List<Object> parms = new ArrayList<Object>(); 
		hql = addWhereBySql(vsrl, hql);
		
		if (vsrl.getSort() != null && vsrl.getOrder() != null) {
			hql += " order by " + vsrl.getSort() + " " + vsrl.getOrder()+" ,s.serial";
		}
		List<Object[]> list = this.saleReturnlistDao.findSql(hql, parms, vsrl.getPage(), vsrl.getRows());
		return checkModelSaleReturn(list);
	}
	

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private String addWhereBySql(VSaleReturnlist vsrl, String hql) {
		Date now = new Date();
		String da = sdf.format(now);
		if(vsrl.getMname()!=null&&!vsrl.getMname().equals("")){ 
			hql += " and s.member_id in (select id from hygl_member where name like '%"+vsrl.getMname().trim()+"%')";
		}
		if(vsrl.getMobile()!=null&&!vsrl.getMobile().trim().equals("")){ 
			hql += " and s.member_id in (select id from hygl_member where mobile like '%"+vsrl.getMobile().trim()+"%')";
		}
		if(vsrl.getRid()!=null){
			hql += " and r.id ="+vsrl.getRid();
		}
		if(vsrl.getType()!=null){
			hql += " and g.type like "+"'%"+vsrl.getType()+"%'";
		}
		if(vsrl.getUid()!=null){
			hql += " and r.user_id ="+vsrl.getUid();
		}
		//计算出截止现在15天之前的日期
		hql += " and s.DATE>=date_sub('"+da+"', interval '15 0:00:00' day_second) ";
		return hql;
	}

	private Long totalSaleReturn(VSaleReturnlist vsrl){
		String hql = " select  count(*) " +
		" from xsgl_salelist_item i ,xsgl_salelist s,rkgl_repertory r,spgl_goods g where i.goods_id=g.id and" +
		" i.salelist_id=s.id and s.repertory_id=r.id " ; 
		hql = addWhereBySql(vsrl, hql);
		
		if (vsrl.getSort() != null && vsrl.getOrder() != null) {
			hql += " order by " + vsrl.getSort() + " " + vsrl.getOrder();
		} 
		return salelistDao.countSQL(hql).longValue();
	}

	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public List<VSaleReturnlist> checkModelSaleReturn(List<Object[]> ob){
		List<VSaleReturnlist> list = new ArrayList<VSaleReturnlist>();
		try {
			for (Object[] o:ob) {
				for (int i = 0; i < o.length; i++) {
					VSaleReturnlist vs = new VSaleReturnlist();
					vs.setBarcode(o[i].toString());
					vs.setType(o[++i].toString());
					vs.setName(o[++i].toString());
					vs.setSize(o[++i].toString());
					vs.setColor(o[++i].toString());
					vs.setDate(sdf2.parse(o[++i].toString()));
					vs.setSerial(o[++i].toString());
					vs.setRid(Long.parseLong(o[++i].toString()));
					vs.setRname(o[++i].toString());
					Object id = o[++i];
					if(id!=null&&!id.equals("")&&!id.equals("null")){
						Integer mid = Integer.parseInt(id.toString());
						String hql = " select name,mobile from hygl_member where id="+mid;
						List<Object[]> mm = salelistDao.findBySql(hql);
						if(mm.size()>0){
							for(Object[] mn:mm){
								for (int j = 0; j < mn.length; j++) {
									vs.setMname(mn[j].toString());
									vs.setMobile(mn[++j].toString());
								}
							}
						}
					}
					list.add(vs);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public DataGrid datagridReturn(VSaleReturnlist model) {
		DataGrid da = new DataGrid();
		da.setRows(findReturn(model));
		da.setTotal(totalReturn(model));
		return da;
	}

	private Long totalReturn(VSaleReturnlist model) { 
		String hql = "select count(*) from spgl_goods g,xsgl_salereturnlist l,xsgl_salereturnlist_item i,spgl_brand b,xsgl_salelist s,rkgl_repertory r" +
		" where l.id=i.salereturnlist_id and r.id=l.repertory_id  and i.goods_id=g.id and g.brand_id=b.id and l.salelist_id=s.id";
		hql = addWhereReturn(model,hql);
		BigInteger a = salelistDao.countSQL(hql);
		return a.longValue();
	}

	private List<VSaleReturnlist> findReturn(VSaleReturnlist model) { 
		String hql = "select l.id ,s.serial saleserial,l.serial ,l.date,r.name rname,Round(l.total,2),l.member_id,l.remark,l.user_id" +
				" from spgl_goods g,xsgl_salereturnlist l,xsgl_salereturnlist_item i,spgl_brand b,xsgl_salelist s,rkgl_repertory r " +
				" where l.id=i.salereturnlist_id and r.id=l.repertory_id  and i.goods_id=g.id and g.brand_id=b.id and l.salelist_id=s.id";
		hql = addWhereReturn(model,hql);
		List<Object> param = new ArrayList<Object>();
		
		List<Object[]> list = saleReturnlistDao.findSql(hql, param, model.getPage(), model.getRows());
		return checkModelReturn(list);
	}

	private List<VSaleReturnlist> checkModelReturn(List<Object[]> list) {
		List<VSaleReturnlist> all = new ArrayList<VSaleReturnlist>();
		try {
			for(Object[] o:list){
				for (int i = 0; i < o.length; i++) {
					VSaleReturnlist e = new VSaleReturnlist();
					e.setId(Long.parseLong(o[i].toString()));
					e.setSaleserial(o[++i].toString());
					e.setSerial(o[++i].toString());
					e.setDate(sdf2.parse(o[++i].toString()));
					e.setRname(o[++i].toString());
					
					Object obj=o[++i];
					
					if(obj!=null){
						Double tmpDouble=Double.parseDouble(obj.toString());
						e.setTotal(tmpDouble);
					}
					
					Object mid = o[++i];
					e.setRemark(o[++i].toString());
					Object uid = o[++i];
					if(mid!=null&&!mid.equals("null")&&!mid.equals("")){
						VMember vm = memberService.getMnameById(Long.parseLong(mid.toString()));
						e.setMname(vm.getName());
					}if(uid!=null&&!uid.equals("null")&&!uid.equals("")){
						VUser vu = userService.findUsernameById(Long.parseLong(uid.toString()));
						e.setUusername(vu.getUsername());
					}
					all.add(e);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return all;
	}

	private String addWhereReturn(VSaleReturnlist model,String hql) {
		if(model.getBeginDate()!=null){
			hql += " and l.date>=date_format('"+sdf.format(model.getBeginDate())+"','%Y%m%d')";
		}if(model.getEndDate()!=null){
			hql += " and l.date<=date_format('"+sdf.format(model.getEndDate())+"','%Y%m%d')";
		}if(model.getMname()!=null&&!model.getMname().trim().equals("")){
			hql += " and l.member_id in (select id from hygl_member where name like '%"+model.getMname()+"%')";
		}if(model.getRid()!=null){
			hql += " and l.repertory_id="+model.getRid();
		}if(model.getSlid()!=null){
			hql += " and r.user_id="+model.getSlid();
		}
		if(model.getUid()!=null){
			hql += " and l.user_id ="+model.getUid();
		} 
		if(model.getGtype()!=null&&!model.getGtype().equals("")){
			hql += " and g.type ='"+model.getGtype()+"'";
		}
		hql += " order by l.date desc";
		return hql;
	}

	@Override
	public List<VGoods> findSaleReturnDetail(VSaleReturnlist model) {
		List<VGoods> vg = new ArrayList<VGoods>();
		if(model.getId()!=null){
			String hql = " select g.id,g.barcode,g.type,g.size,g.color,g.name gname,b.name bname,i.num,i.total from spgl_goods g, " +
					"  xsgl_salereturnlist l,xsgl_salereturnlist_item i,spgl_brand b where l.id=i.salereturnlist_id" +
					"  and i.goods_id=g.id and g.brand_id=b.id and l.id="+model.getId();
			
			List<Object[]> list = salelistDao.findBySql(hql);
			for(Object[] o:list){
				for (int i = 0; i < o.length; i++) {
					VGoods e = new VGoods();
					e.setId(Long.parseLong(o[i].toString()));
					e.setBarcode(o[++i].toString());
					e.setType(o[++i].toString());
					e.setSize(o[++i].toString());
					e.setColor(o[++i].toString());
					e.setName(o[++i].toString());
					e.setBname(o[++i].toString());
					e.setNum(Long.parseLong(o[++i].toString()));
					e.setTotal(Double.parseDouble(o[++i].toString()));
					vg.add(e);
				}
			}
			
		}
		return vg;
	}
	
	
}
